package com.gec.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.util.JdbcUtils;
import com.gec.dao.UserDao;
import com.gec.domain.User;
import com.gec.util.JDBCUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public void save(User user) {
		String sql = "insert into tb_user(name,password,sex)values(?,?,?)";
		String[] strs = {user.getName(),user.getPassword(),user.getSex()};
		
		try {
			JDBCUtil.executeOrUpdate(sql, strs);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), null);
		}
	}

	@Override
	public void delete(Integer id) {
		//准备sql
		String sql = "delete from tb_user where id = ?";
		//参数
		String[] args = {id.toString()};
		//调用方法
		try {
			JDBCUtil.executeOrUpdate(sql, args);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), null);
		}
	}

	@Override
	public void update(User user) {
		//准备Sql
		String sql = "update tb_user set name=?,password=?,sex=? where id = ?";
		//准备参数
		String[] strs = {user.getName(),user.getPassword(),user.getSex(),user.getId().toString()};
		//调用excuteOrupdate
		try {
			JDBCUtil.executeOrUpdate(sql, strs);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), null);
		}
	}

	@Override
	public User getUserById(Integer id) {
		//准备sql
		String sql = "select * from tb_user where id = ?";
		//准备参数 
		String[] strs = {id.toString()};
		ResultSet rs = JDBCUtil.executeQuery(sql, strs);
		try {
			while(rs.next()) {
				//创建用户对象
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), rs);
		}
		
		return null;
	}

	@Override
	public List<User> findAllUsers() {
		//定义用户集合
		List<User> users = new ArrayList<>();
		//准备Sql
		String sql = "select * from tb_user";
		//查询
		ResultSet rs = null;
	    try {
			rs = JDBCUtil.executeQuery(sql, null);
			while(rs.next()) {
				//创建用户对象
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				
				//添加到集合
				users.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), rs);
		}
		return users;
	}

	@Override
	public User login(String name, String password) {
		
		//准备Sql
		String sql = "select * from tb_user where name=? and password=?";
		//将参数放到数组里面
		String[] strs = {name,password};
		ResultSet rs = null;
		try {
			rs = JDBCUtil.executeQuery(sql,strs);
			while(rs.next()) {
				//创建用户对象
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				return user;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeConn(JDBCUtil.getConn(), JDBCUtil.getPs(), rs);
		}
		
		return null;
	}

}
